#include "stdafx.h"

#if (TARGET_OS == OS_POSIX)

#if (OS_COMPILER == CC_GCC)
void __attribute__((used)) so_section_init(void)
{
    asm(".section .init");
    asm(".section .text");
}
void __attribute__((used)) so_section_fini(void)
{
    asm(".section .fini");
    asm(".section .text");
}
#endif

#endif

// #pragma comment(lib, "wsock32.lib")
int main_t(_pid_t pid, int argc, basic_tchar* argv[])
{
    int iRes = 0;
    logi("main_t start");

    char SrcIp[16] = {0x00};
    char DstIp[16] = {0x00};

    // uint32_t SrcAddr = 1694410944;
    // uint32_t DstAddr = 3372132544;

    // uint32_t SrcAddrX = 3232300744;
    // uint32_t DstAddrX = 3232300644;
    // uint32_t SrcAddr = htonl(SrcAddrX);
    // uint32_t DstAddr = htonl(DstAddrX);

    // char tmp[sizeof("255.255.255.255")] = { 0x00 };
    // sprintf(tmp, "%u.%u.%u.%u",((SrcAddr >> 0) & 0xff),
    //	((SrcAddr >> 8) & 0xff),
    //	((SrcAddr >> 16) & 0xff),
    //	((SrcAddr >> 24) & 0xff));

#if (TARGET_OS == OS_WIN)
    const char* code = "{\"component\":[\
	\"{7F17A42A-B5DF-4281-87C3-BFC44486F55C}:runtime:2:2\",\
	\"{F170A724-AACA-4603-BB1C-0A8EAF1C3322}:asynio:3:4\",\
	\"{C54C9CC0-F448-4A49-A622-0467D02E8EB8}:net:5:4\",\
	\"{99103D46-735F-44EE-A6F1-2C94DF20901E}:logs:11:4\",\
	\"{2038d592-5d16-11ed-9b6a-0242ac120002}:license:20:4\",\
	\"{34F9B3BF-6F56-4058-9DCD-04D9A5F0174B}:logs:12:4\",\
	\"{201409F6-22F8-48d3-A69F-7935BDDE6BFF}:msgbus:13:4\"] }";
#else
    const char* code = "{\"component\":[ \
		\"{7F17A42A-B5DF-4281-87C3-BFC44486F55C}:runtime:2:2\",\
		\"{C54C9CC0-F448-4A49-A622-0467D02E8EB8}:net:5:4\",\
		\"{99103D46-735F-44EE-A6F1-2C94DF20901E}:logs:11:4\",\
		\"{2038d592-5d16-11ed-9b6a-0242ac120002}:license:20:4\",\
		\"{34F9B3BF-6F56-4058-9DCD-04D9A5F0174B}:logs:12:4\",\
        \"{F170A724-AACA-4603-BB1C-0A8EAF1C3322}:asynio:3:4\"]}";
#endif

    do {
        HRESULT hr = S_OK;
        basic_tchar path[MAX_PATH + 1] = {0x00};
        get_executepath(pid_null, path, MAX_PATH);
        UINT plen = tstring_strlen(path);

        //////////////////////////////////////////////////////////////////////////
        const char* strguid = "{409C8B8C-8467-47EE-B1BE-4E4112E64926}";
        CContainer com;
        GUID rid;
        rid = StringToGUID(strguid);

        SafePStringPtr containerpath;
        containerpath.ptr = path;
        containerpath.len = plen;

        SafeStringPtr containercode;
        containercode.ptr = code;
        containercode.len = s_strlen(code);

        SafeStringPtr appname;
        appname.ptr = "main";
        appname.len = s_strlen("main");

        logi("LoadContainer");
        hr = com.LoadContainer(&containerpath);
        rc_error(hr == S_OK, S_ERROR);

        logi("CreateContainer");
        hr = com.CreateContainer(rid, &containerpath, &containercode);
        rc_error(hr == S_OK, S_ERROR);

        logi("RegisterContainer");
        hr = com.RegisterContainer(rid, 100, 100);
        rc_error(hr == S_OK, S_ERROR);

        logi("InitContainer");
        hr = com.InitContainer(rid, pid, argv, argc);
        rc_error(hr == S_OK, S_ERROR);

        logi("RunContainer");
        hr = com.RunContainer(rid, 0);
        rc_error(hr == S_OK, S_ERROR);

        logi("UnInitContainer");
        hr = com.UnInitContainer(rid);
        rc_error(hr == S_OK, S_ERROR);

        logi("DestroyContainer");
        iRes = com.GetContainerExitCode(rid);

    } while (iRes == OBJECT_RUN_RET_RESTART);

    logi("main_t end");

    // wait child threads
    // pthread_exit(NULL);

    return 0;
}

#if (TARGET_OS == OS_POSIX)
int main(int argc, char* argv[])
{
    _pid_t pid = get_processid();
    return main_t(pid, argc, argv);
}
#elif (TARGET_OS == OS_MACH)
int main(int argc, char* argv[])
{
    _pid_t pid = get_processid();
    return main_t(pid, argc, argv);
}
#elif (TARGET_OS == OS_UNIX)
int main(int argc, char* argv[])
{
    _pid_t pid = get_processid();
    return main_t(pid, argc, argv);
}
#elif (TARGET_OS == OS_WIN)

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    int iRet = 0;
    int nArgs = 0;
    LPWSTR* szArglist = NULL;

    _pid_t pid = get_processid();

    szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
    iRet = main_t(pid, nArgs, szArglist);
    rc_error(nArgs != 0, 0);
    LocalFree(szArglist);
    return 0;
}
#endif
